//
//  NSObject+LLModelToDic.m
//  TaoTuo
//
//  Created by admin on 2019/8/22.
//  Copyright © 2019 admin. All rights reserved.
//

#import "NSObject+LLModelToDic.h"
#import <objc/runtime.h>

@implementation NSObject (LLModelToDic)


- (NSDictionary *)modelToDic
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    unsigned int count;
    objc_property_t *propertyList = class_copyPropertyList([self class], &count);
    
    for (int i = 0; i < count; i++) {
        objc_property_t property = propertyList[i];
        const char *cName = property_getName(property);
        NSString *name = [NSString stringWithUTF8String:cName];
        NSObject *value = [self valueForKey:name];//valueForKey返回的数字和字符串都是对象
        
        if ([value isKindOfClass:[NSString class]]) {
            //string , bool, int ,NSinteger
            NSString *str = (NSString *)value;
            if (str.length > 0) {
                [dic setObject:value forKey:[NSString stringWithFormat:@"param.%@", name]];
            }
            
        } else if ([value isKindOfClass:[NSNumber class]]) {
           
            [dic setObject:value forKey:[NSString stringWithFormat:@"param.%@", name]];
            
        } else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
            //数组或字典
            [dic setObject:[self arrayOrDicWithObject:value] forKey:name];
            
        }
    }
    
    return [dic copy];
}



//将可能存在model数组转化为普通数组
- (id)arrayOrDicWithObject:(id)origin {
    if ([origin isKindOfClass:[NSArray class]]) {
        //数组
        NSMutableArray *array = [NSMutableArray array];
        for (NSObject *object in origin) {
            if ([object isKindOfClass:[NSString class]] || [object isKindOfClass:[NSNumber class]]) {
                
                if ([object isKindOfClass:[NSString class]]) {
                    NSString *str = (NSString *)object;
                    if (str.length > 0) {
                        [array addObject:object];
                    }
                } else {
                    [array addObject:object];
                }
                
                
            } else if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {
                //数组或字典
                [array addObject:[self arrayOrDicWithObject:object]];
                
            }
        }
        
        return [array copy];
        
    } else if ([origin isKindOfClass:[NSDictionary class]]) {
        //字典
        NSDictionary *originDic = (NSDictionary *)origin;
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        for (NSString *key in originDic.allKeys) {
            id object = [originDic objectForKey:key];
            
            if ([object isKindOfClass:[NSString class]]) {
                //string , bool, int ,NSinteger
                NSString *str = (NSString *)object;
                if (str.length > 0) {
                    [dic setObject:object forKey:[NSString stringWithFormat:@"param.%@", key]];
                }
                
            } else if ([object isKindOfClass:[NSNumber class]]) {
                
                [dic setObject:object forKey:[NSString stringWithFormat:@"param.%@", key]];
                
            } else if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {
                //数组或字典
                [dic setObject:[self arrayOrDicWithObject:object] forKey:key];
                
            }
        }
        
        return [dic copy];
    }
    
    return [NSNull null];

}

@end
